package com.huawei.M01.iterator.M25;



import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

/**
 * 不同节点之间最大差距
 *
 * @author 李志武
 */
public class MaxDiff {
    public int maxAncestorDiff(TreeNode root) {
        // 做层序遍历
        Set<Integer> ans = new HashSet<>();
        preEach(root, ans);
        return ans.stream().sorted().max((Comparator.comparingInt(o -> o))).get();
    }

    private void preEach(TreeNode root, Set<Integer> ans) {
        if (root == null) {
            return;
            }
        getResult(root, ans, root.val);
        preEach(root.left, ans);
        preEach(root.right, ans);

        }

    private void getResult(TreeNode root, Set<Integer> ans, int val) {
        if (root == null) {
            return;
        }
        ans.add(Math.abs(root.val - val));
        getResult(root.left, ans, root.val);
        getResult(root.right,ans, root.val);
        System.out.println(root.val + val);

    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(8);
        TreeNode node1 = new TreeNode();
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
